package com.niuxiaofa.newlaw.Activity;

import android.content.pm.ActivityInfo;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.CourseInfoBean;
import com.niuxiaofa.newlaw.Network.ApiService;
import com.niuxiaofa.newlaw.Network.ServiceManager;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.GlideUtils;
import com.niuxiaofa.newlaw.Tools.JZMediaAliyun;
import com.niuxiaofa.newlaw.Tools.UIUtils;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;
import cn.jzvd.JZDataSource;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 课程详情
 */
public class CourseDetailsActivity extends BaseActivity {


    @BindView(R.id.jzvdstd)
    JzvdStd jzvdstd;

    @BindView(R.id.courseName)
    TextView courseName;


    @BindView(R.id.user_name)
    TextView user_name;

    @BindView(R.id.manuscript)
    TextView manuscript;


    @BindView(R.id.user_img)
    ImageView user_img;




    @Override
    protected int getLayoutId() {
        return R.layout.activity_course_details;
    }

    @Override
    protected void initData() {
        setTitle("全部");

        String id = getIntent().getStringExtra("id");


        Call<CourseInfoBean> call = ServiceManager.getInstance().creat(ApiService.class).queryVideoListByLable(id);

        call.enqueue(new Callback<CourseInfoBean>() {
            @Override
            public void onResponse(Call<CourseInfoBean> call, Response<CourseInfoBean> response) {
                //判断请求是否成功
                if (response.isSuccessful()) {

                    CourseInfoBean baseBean = (CourseInfoBean) response.body();

                    RichText.fromHtml(baseBean.getData().getCoursePartConfList().get(0).getContent()).into(manuscript);


                    GlideUtils.loadNoRoundedImage(getContext(), baseBean.getData().getCourseEntity().getCourseCoverPicUrl(), jzvdstd.posterImageView);


                    GlideUtils.loadUserImg(getContext(),baseBean.getData().getCourseEntity().getUploaderImage(),user_img);

                    courseName.setText(baseBean.getData().getCourseEntity().getCourseName());
                    user_name.setText(baseBean.getData().getCourseEntity().getUploader());
                   // manuscript.setText(baseBean.getData().getCourseEntity().getco);

                    JZDataSource jzDataSource = new JZDataSource(baseBean.getData().getCoursePartConfList().get(0).getUrl());
                    jzvdstd.setUp(jzDataSource, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, JZMediaAliyun.class);
                    jzvdstd.batteryLevel.setVisibility(View.GONE);
                    jzvdstd.startVideo();

                }
            }

            @Override
            public void onFailure(Call<CourseInfoBean> call, Throwable t) {
            Log.e("链接错误", t.getMessage());
                Toast.makeText(UIUtils.getActivity(), "网络连接异常,请稍候在试", Toast.LENGTH_LONG).show();
            }
        });




    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }


}
